# -*- coding:utf-8 -*-

import requests
import re
import sys
import datetime
from multiprocessing import Pool, Manager

key_words = [u'面膜', u'洗面奶', u'花露水', u'化妆棉', u'眼霜', u'杏仁', u'饼干', u'薯片', u'麦片', u'牛肉干', u'巧克力', u'红酒',
             u'伏特加', u'金酒', u'茅台', u'啤酒', u'牛奶', u'鸡蛋', u'猪肋骨', u'鱼', u'土鸡', u'牛排', u'苹果', u'橙子', u'速冻水饺',
             u'NFC果汁', u'玉米油', u'米', u'盐', u'糖', u'酱油', u'醋', u'卷纸', u'抽纸', u'沐浴露', u'洗发水', u'洗洁精', u'洗衣粉',
             u'香皂', u'保鲜膜', u'保鲜袋', u'卫生巾', u'牙膏', u'牙刷', u'毛巾']

pool = None
date = datetime.datetime.now().strftime('%Y%m%d_%H%M%S')


def get_tmall_supermarket_id():
    global pool
    res = Manager().Array('i', [0] * len(key_words))
    file_in = open('../data/ID_TMALL_SUPERMARKET.csv', 'r')
    pool = Pool(processes=10)
    for line in file_in:
        info = line.strip('\n').split(',')
        index = int(info[-1])
        # if index == 1:
        #     break
        sku_id = info[1]
        pool.apply_async(func=multi_get, args=((sku_id, 1), index))
    pool.close()
    file_in.close()
    pool.join()
    get_sum_res(res)
    write_res(res)


def multi_get(sku_store_tuple, index):
    global file_detail, date
    while True:
        try:
            count_num = get_comment_number_by_sku_and_store(sku_store_tuple)
            # print count_num, '=========', sku_store_tuple[0], index
            if count_num is None:
                continue
            else:
                file_detail = open('../data/tmall_supermarket_detail_' + date + '.csv', 'a')
                ss = ','.join([str(count_num), str(sku_store_tuple[0]), str(index)]) + '\n'
                print ss
                file_detail.write(ss)
                file_detail.close()
                break
        except Exception, e:
            print e.message
            print sku_store_tuple


def get_comment_number_by_sku_and_store(sku_store_tuple):
    url = 'https://rate.tmall.com/list_detail_rate.htm?itemId={item_id}&sellerId={seller_id}&currentPage=1&content=0'
    response = requests.get(url.format(item_id=sku_store_tuple[0], seller_id=sku_store_tuple[1]))
    s = response.text.find('\"total\"')
    total_str = str((response.text)[s: s + 20])
    pattern = re.compile(r'"total":(\d+),')
    match = pattern.match(total_str)
    if match:
        return match.group(1)


def get_sum_res(my_res):
    file_res = open('../data/tmall_supermarket_detail_' + date + '.csv', 'r')
    for line in file_res:
        info = line.strip('\n').split(',')
        my_res[int(info[-1])] += int(info[0])
    file_res.close()


def write_res(res):
    global key_words, date
    reload(sys)
    sys.setdefaultencoding('gbk')
    date_now = datetime.datetime.now().strftime('../data/TMALL_SUPERMARKET_' + date + '.csv')
    file_out = open(date_now, 'w')
    file_out.write(','.join(key_words) + '\n')
    file_out.write(','.join([str(i) for i in res]) + '\n')
    file_out.close()


if __name__ == '__main__':
    get_tmall_supermarket_id()
